home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / SAT 2.3.8 / Libraries & Documentation / Tutorial ƒ / Assignment6(add-ons).p < prev    next >
Text File  |  1995-12-08  |  4KB  |  141 lines

  1. {This variant of Assignment6 uses the Add-on lib to simplify the sprite movement.}
  2. {The routine HandleSprite gets a lot simpler this way.}
  3.  
  4. program Assignment6;
  5.     uses
  6. {$ifc UNDEFINED THINK_PASCAL}
  7.         Types, QuickDraw, Menus, Windows, TextEdit, Fonts, Dialogs, Memory, OSEvents, {}
  8. {$endc}
  9.         SAT, SATAddOnLib;
  10.  
  11.     const
  12.         kSpeed = 5;
  13.     var
  14.         ignore: SpritePtr;
  15.         theSound: Handle;
  16.         score: Longint;
  17.         startTime: Longint;
  18.         doneFlag: Boolean;
  19.  
  20.     procedure HandleSprite (me: SpritePtr);
  21.         var
  22.             event: EventRecord;
  23.     begin
  24. {Now hold on to the hat! I'm using GetOSEvent to get key down events.}
  25. {the keydowns then affect the speed variable in the sprite. That speed}
  26. {is added to the position. Finally, I check the position against the screen}
  27. {borders, and modify the speed in case I reach a border.}
  28.  
  29.         if GetOSEvent(keyDownMask, event) then
  30.             if event.what = keyDown then
  31.                 case char(BAnd(event.message, charCodeMask)) of
  32.                     'a': 
  33.                         me^.speed.h := me^.speed.h - 1;
  34.                     's': 
  35.                         me^.speed.h := me^.speed.h + 1;
  36.                     'w': 
  37.                         me^.speed.v := me^.speed.v - 1;
  38.                     'z': 
  39.                         me^.speed.v := me^.speed.v + 1;
  40.                 end;
  41.  
  42. {Now look how much shorter the rest of HandleSprite got!}
  43.         MoveSprite(me);
  44.         if KeepOnScreen(me) then
  45.             ;
  46.     end; {HandleSprite}
  47.  
  48.     procedure SetupSprite (me: SpritePtr);
  49.     begin
  50.         me^.task := @HandleSprite;
  51.         me^.face := SATGetFace(128);
  52.         me^.speed := Point(0);
  53.         SetRect(me^.hotRect, 0, 0, 32, 32);
  54.     end; {SetupSprite}
  55.  
  56.     procedure SetupTarget (me: SpritePtr);
  57.     forward;
  58.  
  59.     procedure HandleTarget (me: SpritePtr);
  60.     begin
  61. {The target sprite isn't modified much. I just limit movement after gSAT.offSizeH instead}
  62. {of a hard-coded constant.}
  63.  
  64. {This wasn't simplified as much since this sprite has such a simple behavior.}
  65.         MoveSprite(me);
  66.         if KeepOnScreen(me) then
  67.             ;
  68.     end; {HandleTarget}
  69.  
  70.     procedure HitTarget (me, him: SpritePtr);
  71.         var
  72.             savePort: SATPort;
  73.             r: Rect;
  74.     begin
  75.         if him^.task = @HandleSprite then {Check what we hit!}
  76.             begin
  77.                 me^.task := nil;
  78.                 ignore := SATNewSprite(-1, 0, SATRand(gSAT.offSizeV - 32), @SetupTarget);
  79. {We could also re-use the old sprite for a new one, if we like.}
  80.                 SATSoundPlay(theSound, 1, true);
  81.  
  82. {Add to the score}
  83.                 score := score + 1;
  84.  
  85. {Draw the score on the screen.}
  86.                 SATGetPort(savePort); {Save port and device!}
  87.                 SATSetPortBackScreen;
  88.                 SetRect(r, 100, 0, 100 + stringWidth(stringof('Caught: ', score : 1)), 15);
  89.                 EraseRect(r);
  90.                 MoveTo(r.left, r.bottom - 3);
  91.                 DrawString(stringof('Caught: ', score : 1));
  92.                 SATBackChanged(r);
  93.                 SATSetPort(savePort); {Always restore!}
  94.  
  95.                 if score = 10 then
  96.                     doneFlag := true;
  97.             end;
  98.     end; {HitTarget}
  99.  
  100.     procedure SetupTarget (me: SpritePtr);
  101.     begin
  102.         me^.task := @HandleTarget;
  103.         me^.hitTask := @HitTarget;
  104.         me^.face := SATGetFace(129);
  105.         SetRect(me^.hotRect, 0, 0, 32, 32);
  106.         me^.speed.h := kSpeed;
  107.     end; {SetupTarget}
  108.  
  109.     const
  110.         kTicksPerFrame = 2;
  111.     var
  112.         t: Longint;
  113.  
  114. begin
  115. {If we don't use Think Pascal, we must make standard inits ourselves.}
  116. {$ifc UNDEFINED THINK_PASCAL}
  117.     SATInitToolbox;
  118. {$endc}
  119.  
  120.     SATConfigure(false, kVPositionSort, kForwardCollision, 32);
  121.     SATInit(128, 129, 478, 302);
  122.     ignore := SATNewSprite(0, 200, 200, @SetupSprite);
  123.     ignore := SATNewSprite(0, 0, SATRand(gSAT.offSizeV), @SetupTarget);
  124.     theSound := SATGetNamedSound('TestSound');
  125.     score := 0;
  126.     HideCursor;
  127.     startTime := TickCount;
  128.     doneFlag := false;
  129.     while not Button and not doneFlag do
  130.         begin
  131.             t := TickCount;
  132.             SATRun(true);
  133.             while TickCount < t + kTicksPerFrame do
  134.                 ;
  135.         end;
  136.     ShowCursor;
  137. {Extremely simple result report. A real game should, of course, display this in the game}
  138. {window, in a prettier way, perhaps with a high score list, etc.}
  139.     SATReportStr(stringof('Time to catch ', score : 1, ' disks: ', (TickCount - startTime) div 60 : 1, ' seconds.'));
  140.     SATSoundShutup;
  141. end.